package com.wanxi.servlet;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ProductDTO;
import com.wanxi.model.ProductModel;
import com.wanxi.service.ProductService;
import com.wanxi.service.impl.ProductServiceImpl;
import com.wanxi.util.Tool;
import com.wanxi.vo.ProductVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/product/findById")
public class ProductFindByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        1.乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的数据
        String id = req.getParameter("id");
//        3.封装到model
        ProductVO productVO = new ProductVO();
        productVO.setId(Tool.strToInt(id));
//        4.调用服务逻辑层
        ProductService productService = new ProductServiceImpl();
//        5.得到返回值
        List<ProductDTO> productDTOList = productService.findById(productVO);
//        6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(productDTOList));

    }
}
